export function validateUSCC(code) {
	var patrn = /^[0-9A-Z]+$/;
	//18位校验及大写校验
	if (code.length != 18 || patrn.test(code) == false) {
		return '不是有效的统一社会信用编码';
	} else {
		var Ancode; //统一社会信用代码的每一个值
		var Ancodevalue; //统一社会信用代码每一个值的权重
		var total = 0;
		var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]; //加权因子
		var str = '0123456789ABCDEFGHJKLMNPQRTUWXY';
		//不用I、O、S、V、Z
		for (var i = 0; i < code.length - 1; i++) {
			Ancode = code.substring(i, i + 1);
			Ancodevalue = str.indexOf(Ancode);
			total = total + Ancodevalue * weightedfactors[i];
			//权重与加权因子相乘之和
		}
		var logiccheckcode = 31 - (total % 31);
		if (logiccheckcode == 31) {
			logiccheckcode = 0;
		}
		var Str = '0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y';
		var Array_Str = Str.split(',');
		logiccheckcode = Array_Str[logiccheckcode];

		var checkcode = code.substring(17, 18);
		if (logiccheckcode != checkcode) {
			return '不是有效的统一社会信用编码';
		}
		return true;
	}
}

export function validateUSCC2(CODE) {
	console.log(CODE, 'CODE');
	if (CODE.length != 9) {
		// alert("组织机构代码位数不正确");
		return '组织机构代码位数不正确';
	} else {
		var patrn = /^[0-9A-Z]+$/;
		if (patrn.test(CODE) == false) {
			// alert("组织机构代码只可为数字或大写拉丁字母");
			return '组织机构代码只可为数字或大写拉丁字母';
		} else {
			var lastpatrn = /^[0-9X]+$/;
			var checkcode = CODE.substring(8, 9);
			if (lastpatrn.test(checkcode) == false) {
				// alert("组织机构代码最后一位只可为数字或大写拉丁字母:X");
				return '组织机构代码最后一位只可为数字或大写拉丁字母:X';
			} else {
				var ancode;
				var ancodevalue;
				var total = 0;
				var weightedfactors = [3, 7, 9, 10, 5, 8, 4, 2]; //加权因子
				var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
				for (var i = 0; i < CODE.length - 1; i++) {
					ancode = CODE.substring(i, i + 1);
					ancodevalue = str.indexOf(ancode);
					total = total + ancodevalue * weightedfactors[i];
				}
				var logiccheckcode = 11 - (total % 11);
				if (logiccheckcode == 10) logiccheckcode = 'X';
				if (logiccheckcode == 11) logiccheckcode = '0';
				if (checkcode != logiccheckcode) {
					// alert("组织机构代码结构错误.最后一位校验码应为:" + logiccheckcode);
					// return "组织机构代码结构错误.最后一位校验码应为:" + logiccheckcode;
					return '组织机构代码结构错误.';
				} else {
					// alert("组织机构代码正确");
					return true;
				}
			}
		}
	}
}

function isValidDate(year, month, day) {
	const date = new Date(year, month - 1, day);
	return date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day;
}

function getCheckCode(str) {
	const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
	const codes = '10X98765432'.split('');
	let sum = 0;
	for (let i = 0; i < 17; i++) {
		sum += str[i] * weights[i];
	}
	return codes[sum % 11];
}

export function isValidIdCard(idCard) {
	const reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
	if (!reg.test(idCard)) {
		return false;
	}

	const year = parseInt(idCard.substr(6, 4), 10);
	const month = parseInt(idCard.substr(10, 2), 10);
	const day = parseInt(idCard.substr(12, 2), 10);
	if (!isValidDate(year, month, day)) {
		return false;
	}

	const checkCode = idCard[17].toUpperCase();
	if (checkCode !== getCheckCode(idCard)) {
		return false;
	}

	return true;
}